package com.polaris.live.adapter.home

import android.view.View
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.constant.AppConst
import com.polaris.live.databinding.AdapterFriendShipRegionBinding
import com.polaris.live.resp.back_resp.RegionDataResp
import com.polaris.live.resp.back_resp.RegionResp
import com.polaris.live.widget.country.countryIcons

/**
 * FriendShipRegionAdapter
 *
 * @author Created by 半仙 on 2024/10/9/009 16:40
 */
class FriendShipRegionAdapter : BaseBindingAdapter<AdapterFriendShipRegionBinding, RegionResp>() {

    private var mSelectPosition = 0

    override fun convert(holder: VBViewHolder<AdapterFriendShipRegionBinding>, item: RegionResp) {
        holder.vb.apply {
            if (item.region == null || item.region == -1) {
                ivCountry1.setImageResource(R.drawable.friend_ship_region_all)
                ivCountry2.visibility = View.GONE
                ivCountry3.visibility = View.GONE
            } else {
                // 创建一个图标视图的列表
                val countryViews = listOf(ivCountry1, ivCountry2, ivCountry3)
                countryViews.forEachIndexed { index, imageView ->
                    val countryIcon = countryIcons[item.showCountries.getOrNull(index)]
                    if (countryIcon != null) {
                        imageView.visibility = View.VISIBLE
                        imageView.setImageResource(countryIcon)
                    } else {
                        imageView.visibility = View.GONE
                    }
                }
            }
            tvRegionContent.text = item.content
            if (mSelectPosition == holder.absoluteAdapterPosition) {
                consRegion.setBackgroundResource(R.drawable.friend_ship_tab_select_back)
            } else {
                consRegion.setBackgroundResource(R.drawable.friend_ship_tab_not_select_back)
            }
        }
    }

    fun setSelectIndex(positon: Int) {
        mSelectPosition = positon
        notifyDataSetChanged()
    }


    private var mIsFirst = AppConst.NO

    fun setDefualt(region: RegionDataResp) {

        if (mIsFirst == AppConst.YES) return
        mIsFirst = AppConst.YES

        if (region.default != null) {
            val defaultIndex = region.regions.indexOfFirst { it.region == region.default }
            if (defaultIndex != -1) {
                setSelectIndex(defaultIndex)
            } else {
                setSelectIndex(0)
            }
        } else {
            setSelectIndex(0)
        }
    }


    fun getSelectData() = data.getOrNull(mSelectPosition)

}